<?php
/*
	消息通知设置
	参数：$msgtype,$text,$tpldata,$uid,$url,$color
*/
defined('BY_JYA') or exit('error');
list($msgtype,$uid,$store_id,$order_id)=$arguments;
$store=pdo_get('jy_qiuyuyue_store',array('id'=>$store_id,'uniacid'=>$_SESSION['uniacid']));
$order=pdo_get('jy_qiuyuyue_order',array('id'=>$order_id,'uniacid'=>$_SESSION['uniacid']));
$order['createtime'] = date('Y/m/d H:i:s',$order['createtime']);
$self=pdo_get('jy_qiuyuyue_order_self',array('order_id'=>$order_id,'uniacid'=>$_SESSION['uniacid']));
$out=pdo_get('jy_qiuyuyue_order_out',array('order_id'=>$order_id,'uniacid'=>$_SESSION['uniacid']));
$userinfo=pdo_get('core_members',array('id'=>$uid,'uniacid'=>$_SESSION['uniacid']));
$out['out_time'] = $out['out_time']?$out['out_time']:'即时派送';
$goods=pdo_getall('jy_qiuyuyue_order_goods',array('order_id'=>$order_id));
if($goods){
	$str = '';
	foreach($goods as $g){
		$g['goods_package']=$g['goods_package']?json_decode($g['goods_package'],true):array();
		if(empty($g['goods_package'])){
			$money = $g['goods_price'] * $g['goods_num'];
			$str .= ''.str_pad($g['goods_title'], '26', ' ', STR_PAD_RIGHT);
			$str .= ''.str_pad($g['goods_num'], '5', ' ', STR_PAD_RIGHT);
			$str .= ''. number_format($money, 2) . "元\n";
		}else{
			$str .= $g['goods_title']."--规格如下：\n";
			foreach($g['goods_package'] as $p){
					$money = $p['price'] * $p['num'];
					$str .= '['.str_pad($p['title'].']', '23', ' ', STR_PAD_RIGHT);
					$str .= ''.str_pad($p['num'], '5', ' ', STR_PAD_RIGHT);
					$str .= ''. number_format($money, 2) . "元\n";
					
			}
		}
	}
}
$goodsinfo = "名称　                数量     单价\n";
$goodsinfo .= $str;
$goodsinfo .= "-----------------------------\n";
$order_type=$this->type__order();
$order_type=$order_type[$order['order_type']];
$selfinfo=pdo_get('jy_qiuyuyue_order_self',array('order_id'=>$order_id));
$is_pays =array('0'=>'未支付','1'=>'已支付');
if($msgtype == 'order_new' && $order['order_type'] == 1){
	$url=custom_url('jy_qiuyuyue/order_show/',true);
	$tpldata=array(
		'first'=>array('value'=>'您好，您的堂点单，已下单成功','color'=>''),
		'keyword1'=>array('value'=>$store['title'],'color'=>'#ff510'),
		'keyword2'=>array('value'=>date('Y-m-d h:i:s',TIMESTAMP),'color'=>'#ff510'),
		'keyword3'=>array('value'=>"\n".$goodsinfo,'color'=>'#ff510'),
		'keyword4'=>array('value'=>$order['price'],'color'=>'#ff510'),
		'remark'=>array('value'=>'查看更多订单信息~!','color'=>'#ff510'),
	);
	$order['table']=pdo_get('jy_qiuyuyue_order_table',array('order_id'=>$order['id']));
	$staffdata=array(
		'first'=>array('value'=>'收到一个新的订单，请及时处理！','color'=>''),
		'tradeDateTime'=>array('value'=>$order['createtime'],'color'=>'#ff510'),
		'customerInfo'=>array('value'=>$userinfo['nickname'].' '.$userinfo['mobile'],'color'=>'#ff510'),
		'orderType'=>array('value'=>$order_type."\n".$goodsinfo,'color'=>'#ff510'),
		'orderItemName'=>array('value'=>'桌号','color'=>'#ff510'),
		'orderItemData'=>array('value'=>$order['table']['table_id'],'color'=>'#ff510'),
		'remark'=>array('value'=>'查看更多订单信息~!','color'=>'#ff510'),
	);
	$staff_tpl_id=cfc('account')->get_tpl_template_id('TM00351','新订单通知');
	
}elseif($msgtype == 'order_pay'){
	$url=custom_url('jy_qiuyuyue/order_show/',true);
	$tpldata=array(
				'first'=>array('value'=>'您好，您有一个订单状态已更新','color'=>''),
				'keyword1'=>array('value'=>$order['ordersn'],'color'=>'#ff510'),
				'keyword2'=>array('value'=>'已付款','color'=>'#ff510'),
				'keyword3'=>array('value'=>$order['createtime'],'color'=>'#ff510'),
				'remark'=>array('value'=>'更多消息请查看订单详情','color'=>'#ff510'),
	);
	$staffdata=array(
		'first'=>array('value'=>'有一个客户订单已支付，请及时处理！','color'=>''),
		'keyword1'=>array('value'=>$order['ordersn'],'color'=>'#ff510'),
		'keyword2'=>array('value'=>'已付款','color'=>'#ff510'),
		'keyword3'=>array('value'=>$order['createtime'],'color'=>'#ff510'),
		'remark'=>array('value'=>'更多消息请查看订单详情','color'=>'#ff510'),
	);
	$staffmsg['msgtype']='news';
	$staf[0]['title'] = "您好，您有一个订单状态已更新\n";
	$staf[0]['description'] ='订单编号:'.$order['ordersn']."\n";
	$staf[0]['description'] .="下单状态:已付款\n";
	$staf[0]['description'] .='订单时间:'.$order['createtime']."\n";
	$staf[0]['description'] .='查看更多订单信息~!';
	$staf[0]['URL'] =$url;
	$staffmsg['news']['articles'] =$staf;
	
	$newsmsg['msgtype']='news';
	$articles[0]['title'] = "您好，您有一个订单状态已更新\n";
	$articles[0]['description'] ='订单编号:'.$order['ordersn']."\n";
	$articles[0]['description'] .="下单状态:已付款\n";
	$articles[0]['description'] .='订单时间:'.$order['createtime']."\n";
	$articles[0]['description'] .='查看更多订单信息~!';
	$articles[0]['URL'] =$url;
	$newsmsg['news']['articles'] =$articles;
	$this->sendstaff('kf_order_pay',$staffmsg,$staffdata,$uid,$url,$color,$store_id);
	
}elseif($msgtype == 'order_new' && $order['order_type'] == 2){
	$url=custom_url('jy_qiuyuyue/order_show/',true);
	$tpldata=array(
		'first'=>array('value'=>'您好，您的外卖单，已下单成功','color'=>''),
		'keyword1'=>array('value'=>$store['title'],'color'=>'#ff510'),
		'keyword2'=>array('value'=>date('Y-m-d h:i:s',TIMESTAMP),'color'=>'#ff510'),
		'keyword3'=>array('value'=>"\n".$goodsinfo,'color'=>'#ff510'),
		'keyword4'=>array('value'=>$order['price'],'color'=>'#ff510'),
		'remark'=>array('value'=>'约45分钟后送达~！','color'=>'#ff510'),
	);
	$staffdata=array(
		'first'=>array('value'=>'收到一个新的订单，请及时处理！','color'=>''),
		'tradeDateTime'=>array('value'=>$order['createtime'],'color'=>'#ff510'),
		'customerInfo'=>array('value'=>$userinfo['nickname'].' '.$userinfo['mobile'],'color'=>'#ff510'),
		'orderType'=>array('value'=>$order_type."\n".$goodsinfo,'color'=>'#ff510'),
		'orderItemName'=>array('value'=>'配送信息','color'=>'#ff510'),
		'orderItemData'=>array('value'=>"{$out['receiver_name']}/{$out['receiver_mobile']}/{$out['receiver_address']}",'color'=>'#ff510'),
		'remark'=>array('value'=>'查看更多订单信息~!','color'=>'#ff510'),
	);
	$staff_tpl_id=cfc('account')->get_tpl_template_id('TM00351','新订单通知');
}elseif($msgtype == 'salf_order_new' && $order['order_type'] == 3){
	$url=custom_url('jy_qiuyuyue/order_show/',true);
	$tpldata=array(
		'first'=>array('value'=>'下单成功,请及时支付！','color'=>''),
		'keyword1'=>array('value'=>$order['ordersn'],'color'=>'#ff510'),
		'keyword2'=>array('value'=>$self['selfnumber'],'color'=>'#ff510'),
		'keyword3'=>array('value'=>$store['title'],'color'=>'#ff510'),
		'keyword4'=>array('value'=>"\n".$goodsinfo,'color'=>'#ff510'),
		'keyword5'=>array('value'=>$order['price'],'color'=>'#ff510'),
		'remark'=>array('value'=>'祝您用餐愉快~！','color'=>'#ff510'),
	);
	$newsmsg['msgtype']='news';
	$articles[0]['title'] = "下单成功,请及时支付！\n";
	$articles[0]['description'] ='订单编号:'.$order['ordersn']."\n";
	$articles[0]['description'] .='取餐号:'.$self['selfnumber']."\n";
	$articles[0]['description'] .='餐厅:'.$store['title']."\n";
	$articles[0]['description'] .='金额:'.$order['price']."\n";
	$articles[0]['description'] .="【商品详情】\n";
	$articles[0]['description'] .="-----------------------------\n";
	$articles[0]['description'] .=$goodsinfo."\n";
	$articles[0]['description'] .='祝您用餐愉快~！';
	$articles[0]['URL'] =$url;
	$newsmsg['news']['articles'] =$articles;
}elseif($msgtype == 'yuding_order_new' && $order['order_type'] == 5){
	$url=custom_url('jy_qiuyuyue/order_show/',true);
	$tpldata=array(
		'first'=>array('value'=>'预定成功！','color'=>''),
		'keyword1'=>array('value'=>$store['title'],'color'=>'#ff510'),
		'keyword2'=>array('value'=>date('Y-m-d h:i:s',TIMESTAMP),'color'=>'#ff510'),
		'keyword3'=>array('value'=>$order['ordersn'],'color'=>'#ff510'),
		'keyword4'=>array('value'=>$order_type,'color'=>'#ff510'),
		'remark'=>array('value'=>'期待您的光临~！','color'=>'#ff510'),
	);
	$newsmsg['msgtype']='news';
	$articles[0]['title'] = "预定成功！\n";
	$articles[0]['description'] ='餐厅:'.$store['title']."\n";
	$articles[0]['description'] .='预定时间:'.$order['createtime']."\n";
	$articles[0]['description'] .='订单编号:'.$order['ordersn']."\n";
	$articles[0]['description'] .='期待您的光临~!';
	$articles[0]['URL'] =$url;
	$newsmsg['news']['articles'] =$articles;
}elseif($msgtype == 'call'){
	$url=custom_url('jy_qiuyuyue/queue/',true);
	$callmember = pdo_get('jy_qiuyuyue_queue_member',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id,'id'=>$order_id));
	$callmember['createtime'] = date('Y/m/d H:i:s',$callmember['createtime']);
	$tpldata=array(
				'first'=>array('value'=>'排号通知','color'=>''),
				'keyword1'=>array('value'=>$callmember['number'],'color'=>'#ff510'),
				'keyword2'=>array('value'=>$callmember['createtime'],'color'=>'#ff510'),
				'remark'=>array('value'=>'快到您啦，快来就餐吧~！','color'=>'#ff510'),
	);
	$newsmsg['msgtype']='news';
	$articles[0]['title'] = "排号通知\n";
	$articles[0]['description'] ='当前排号:'.$callmember['number']."\n";
	$articles[0]['description'] .='取号时间:'.$callmember['createtime']."\n";
	$articles[0]['description'] .='快到您啦，快来就餐吧~！';
	$articles[0]['URL'] =$url;
	$newsmsg['news']['articles'] =$articles;
}elseif($msgtype == 'queue_update'){
	$url=custom_url('jy_qiuyuyue/queue/',true);
	$mymember = pdo_get('jy_qiuyuyue_queue_member',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id,'id'=>$order_id));
	$dmember = pdo_count('jy_qiuyuyue_queue_member',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id,'status'=>0,'id <'=>$order_id));
	$tpldata=array(
				'first'=>array('value'=>'排号通知','color'=>''),
				'keyword1'=>array('value'=>$store['title'],'color'=>'#ff510'),
				'keyword2'=>array('value'=>$mymember['number'],'color'=>'#ff510'),
				'keyword3'=>array('value'=>$dmember,'color'=>'#ff510'),
				'remark'=>array('value'=>'祝您用餐愉快~！','color'=>'#ff510'),
	);
	$newsmsg['msgtype']='news';
	$articles[0]['title'] = "排号通知\n";
	$articles[0]['description'] ='餐厅:'.$callmember[0]['number']."\n";
	$articles[0]['description'] .='您的排号:'.$mymember['number']."\n";
	$articles[0]['description'] .='还需等待:'.$dmember."\n";
	$articles[0]['description'] .='祝您用餐愉快~！';
	$articles[0]['URL'] =$url;
	$newsmsg['news']['articles'] =$articles;
}

$this->send_message($msgtype,$newsmsg,$tpldata,$uid,$url,$color);
if($staffdata && $staff_tpl_id){
	$staff=pdo_getall('jy_qiuyuyue_staff',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id,'status'=>1),'mid');
	if($staff){
		foreach($staff as $s){
			model('core/account')->sendTplNotice($s['mid'],$staff_tpl_id,$staffdata,$url);
		}
	}
}